﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMS.HelperClasses;
using WHMS.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using WHMS;
using WHMS.EntityClasses;

namespace WMS
{
	public partial class AuditForm : Form
	{
		public AuditForm()
		{
			InitializeComponent();

			LoadData();
		}

		void LoadData()
		{
			FormsHelper.LoadComboBox(StoresDDL, StoreFields.Id, StoreFields.Name, (x, y) => DALHelper.GetDynamicList(x, y, null));			
		}

		private int GetSelectedStoreId()
		{
			var storeId = (int)((System.Data.DataRowView)(StoresDDL.SelectedItem)).Row.ItemArray[0];
			return storeId;
		}

		private void SaveBtn_Click(object sender, EventArgs e)
		{
			SaveResult.Text =  DALHelper.SaveCollection(_inventoryItems).ToString();
		}

		private void StartCountBtn_Click(object sender, EventArgs e)
		{
			var storeId = GetSelectedStoreId();
			if (storeId <= 0)
				return;

			SaveResult.Text = "0";

			var inventoryId = Business.PopulateInventory(storeId, AdditionDateDTP.Value);

			var prefetch = new PrefetchPath2(EntityType.InventoryItemEntity);
			prefetch.Add(InventoryItemEntity.PrefetchPathItem).SubPath.Add(ItemEntity.PrefetchPathUnit_);
			prefetch.Add(InventoryItemEntity.PrefetchPathShelf);

			DALHelper.FetchCollection(_inventoryItems, new RelationPredicateBucket(InventoryItemFields.InventoryId == inventoryId), prefetch);

			if (_inventoryItems.Count <= 0)
			{
				MessageBox.Show("لا توجد أصناف بالمخزن");
			}
		}

		private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			var columnIndex = (int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex);

			e.Control.KeyPress -= new System.Windows.Forms.KeyPressEventHandler(this.Control_KeyPress);

			if (columnIndex != quantityCountedDataGridViewTextBoxColumn.Index)
				return;
	
			e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress);

		}

		void Control_KeyPress(object sender, KeyPressEventArgs e)
		{
			bool valid = true;

			if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
			{
				valid = false;
			}

			e.Handled = valid;
		}

		private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex != quantityCountedDataGridViewTextBoxColumn.Index)
				return;

			var itemIdValue = dataGridView2.Rows[e.RowIndex].Cells[itemIdDataGridViewTextBoxColumn.Index].Value;
			if (itemIdValue == null)
				return;

			var form = new UnitConverterForm((int)itemIdValue);
			if (form.ShowDialog() == DialogResult.OK)
			{
				dataGridView2.SuspendLayout();
				dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = form.Value;
				dataGridView2.UpdateCellValue(e.ColumnIndex, e.RowIndex);
				dataGridView2.ResumeLayout(true);
			}
		}
	}
}
